package com.smx.controller;

import com.smx.dao.BookDao;
import com.smx.model.entity.BorrowBooks;
import com.smx.model.vo.BooksVo;
import com.smx.model.vo.BorrowBooksVo;
import com.smx.mvc.ResponseBody;
import com.smx.service.BooksService;
import com.smx.util.DateFormatUtil;
import com.smx.util.JsonUtil;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName BooksController
 * @Description TODO
 * @Author moses
 * @Date 2021/5/10
 **/
public class BooksController {
    @ResponseBody("/getAllBooks.do")
    public String getAllBooksByPage(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {

        Integer pageNum = Integer.valueOf(req.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));

        List<BooksVo> list = BooksService.getAlByPage((pageNum - 1) * pageSize, pageSize);
        req.getSession().setAttribute("books", list);
        return BookDao.getAllCount() + "";
    }

    @ResponseBody("/store.do")
    public String storeBook(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {
        String bookId = req.getParameter("bookId");
        String userId = req.getParameter("userId");
        if (null != bookId && null != userId) {
            try {
                int i = BooksService.addStoreId(Integer.valueOf(bookId), Integer.valueOf(userId));
                return i + "";

            } catch (NumberFormatException e) {
                return 0 + "";
            }

        }
        return 0 + "";
    }

    @ResponseBody(("/cancelStore.do"))
    public String cancelStoreBook(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {
        String bookId = req.getParameter("bookId");
        String userId = req.getParameter("userId");
        if (null != bookId && null != userId) {
            try {
                int i = BooksService.removeStore(Integer.valueOf(bookId), Integer.valueOf(userId));
                return i + "";

            } catch (NumberFormatException e) {
                return 0 + "";
            }

        }
        return 0 + "";
    }

    @ResponseBody("/getStoreBooks.do")
    public String getStoreBook(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {
        String userId = req.getParameter("userId");
        if (null != userId) {
            try {
                Integer pageNum = Integer.valueOf(req.getParameter("pageNum"));
                Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
                List<BooksVo> storeBooks = BooksService.getStoreBookByUserId(Integer.valueOf(userId), pageNum, pageSize);
                req.getSession().setAttribute("storeBooks", storeBooks);
                return BooksService.getStoreBookByUserId(Integer.valueOf(userId), 0, 0).size() + "";
                //return JsonUtil.toJSON(storeBooks);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return 0 + "";
    }


    @ResponseBody("/getBorrowBooks.do")
    public String getBorrowBooks(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {
        String userId = req.getParameter("userId");
        if (null != userId) {
            try {
                Integer pageNum = Integer.valueOf(req.getParameter("pageNum"));
                Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
                List<BorrowBooksVo> borrowBooks = BooksService.getBorrowBooksByUserId(Integer.valueOf(userId), (pageNum - 1) * pageSize, pageSize);
                req.getSession().setAttribute("borrowBooks", borrowBooks);
                return BooksService.getBorrowBooksCountByUserId(Integer.valueOf(userId)) + "";
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return 0 + "";
    }

    @ResponseBody("/insertBorrowBook.do")
    public String insertBorrowBook(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {
        String bookId = req.getParameter("bookId");
        Date borrowDate = new Date();
        String endDate = req.getParameter("endDate");
        String userId = req.getParameter("userId");

        int i = 0;
        try {
            i = BooksService.insertBorrowBook(BorrowBooks.builder().bookId(Integer.valueOf(bookId)).borrowDate(borrowDate)
                    .endDate(new SimpleDateFormat("yyyy-MM-dd").parse(endDate)).userId(Integer.valueOf(userId)).build());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return i + "";
    }

    @ResponseBody("/historyBorrowBook.do")
    public String historyBorrowBook(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {
        String userId = req.getParameter("userId");
        if (null != userId) {
            try {
                Integer pageNum = Integer.valueOf(req.getParameter("pageNum"));
                Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
                List<BorrowBooksVo> historyBorrowBooks = BooksService.getHistoryBorrowBooksByUserIdPage(Integer.valueOf(userId), (pageNum-1)*pageSize, pageSize);
                req.getSession().setAttribute("historyBorrowBooks",historyBorrowBooks);
                return BooksService.getHistoryBorrowBooksCountByUserId(Integer.valueOf(userId))+"";
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return 0 + "";
    }

    @ResponseBody(("/returnBorrowBook.do"))
    public String returnBorrowBook(Servlet servlet, HttpServletRequest req, HttpServletResponse response) {
        String illegal = req.getParameter("illegal");
        String bookId = req.getParameter("bookId");
        String userId = req.getParameter("userId");
        if (null != bookId && null != userId) {
            try {
                int i = BooksService.updateBorrowBook(illegal,Integer.valueOf(bookId), Integer.valueOf(userId));
                return i + "";

            } catch (NumberFormatException e) {
                return 0 + "";
            }

        }
        return 0 + "";
    }

}
